package cn.pzhu.pserson.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.pzhu.pserson.util.jssdk;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import cn.pzhu.pserson.dto.WxconfigDTO;
import cn.pzhu.pserson.util.ResponseUtil;
import cn.pzhu.pserson.service.IWxconfigService;
import cn.pzhu.pserson.dto.LimitShowDTO;
import cn.pzhu.pserson.model.Wxconfig;


@Controller
@RequestMapping("/wxconfig")
public class WxconfigController {
    @Resource(name="wxconfigService")
    private IWxconfigService wxconfigService;



    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseEntity<String> list(
        @Validated
        LimitShowDTO limitShowDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        Map<String, Object> result = new HashMap<String, Object>();

        List<Map<String, Object>> wxconfigList = wxconfigService.loadMaps(null, null, null, null, limitShowDTO.getPageNum(), limitShowDTO.getPageSize());
        long count = wxconfigService.count();

        result.put("rows", wxconfigList);
        result.put("total", count);

        return ResponseUtil.getResEntityForGetAndJson(result);
    }

    @CrossOrigin
    @RequestMapping(value = "/getConfig", method = RequestMethod.POST, produces = {"text/plain;charset=utf-8","text/html;charset=utf-8"}             )
    @ResponseBody
    public ResponseEntity<String> list(@Validated LimitShowDTO limitShowDTO, BindingResult errors, HttpServletRequest request, HttpServletResponse response) throws Exception {

        HashMap<String, Object> map =  new HashMap<String, Object>();
        map.put("data", jssdk.jsSDK_Sign(request.getParameter("url")));

        ResponseEntity<String> stringResponseEntity = ResponseUtil.getResEntityForGetAndJson(map);
        return stringResponseEntity;
    }



    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<String> show() {
        Map<String, Object> wxconfigModel = wxconfigService.findMap();

        return ResponseUtil.getResEntityForGetAndJson(wxconfigModel);
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseEntity<String> create(
        @Validated
        WxconfigDTO wxconfigDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        Wxconfig wxconfigModel = wxconfigDTO.toModel();

        return ResponseUtil.getResEntityForPPP(wxconfigService.save(wxconfigModel));
    }

    @RequestMapping(value = "", method = RequestMethod.PUT)
    public ResponseEntity<String> update(
        @Validated
        WxconfigDTO wxconfigDTO, BindingResult errors) {

        Wxconfig wxconfigModel = wxconfigDTO.toModel();
        return ResponseUtil.getResEntityForPPP(wxconfigService.update(wxconfigModel));
    }

    @RequestMapping(value = "", method = RequestMethod.DELETE)
    public ResponseEntity<String> destory() {
        return ResponseUtil.getResEntityForDel(wxconfigService.delete());
    }

}
